package com.jiaobuchong.beanstalkd;

import com.dinstone.beanstalkc.BeanstalkClientFactory;
import com.dinstone.beanstalkc.Configuration;
import com.dinstone.beanstalkc.Job;
import com.dinstone.beanstalkc.JobConsumer;

import java.util.Objects;

/**
 * @author zhoujie
 * Created on 2018-07-14 12:07
 */
public class BeanstalkConsumer {
    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.setServiceHost("127.0.0.1");
        config.setServicePort(11300);
        BeanstalkClientFactory factory = new BeanstalkClientFactory(config);
        JobConsumer consumer = factory.createJobConsumer("beanstalkd-demo");
        while (true) {
//            System.out.println("ddd");
            // reserveJob 有一个超时时间参数，单位是秒，表示获取消息最多花费多长时间
            Job job = consumer.reserveJob(3);
            if (Objects.isNull(job)) {
                continue;
            }
            System.out.println(job.getId());
            System.out.println(new String(job.getData()));
//            consumer.deleteJob(job.getId());
        }

    }
}
